package com.rainbowelephant.fun.fitness;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.Toast;

public class ChecklistActivity extends Activity {

	String [] values = new String[0];

	OnKeyListener keyListen = new OnKeyListener(){

		public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
			if(arg1 == KeyEvent.KEYCODE_ENTER){
				addNew(arg0);
				return true;
			}
			return false;
		}
		
	};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checklist);
        makeMenu();
//        setContentView(R.layout.activity_checklist);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_checklist, menu);
        return true;
    }
    
    public void clicked(View v){
		String text = ((CheckBox)v).getText() + "";
		int i = 0;
		String [] newValues = new String[values.length-1];
		for(String t : values){
			if(!t.equals(text)){
				newValues[i++] = t;
			}
		}
		values = newValues;
		v.setEnabled(false);
    }
    
    public void addNew(View v){
    	EditText edit = (EditText) v;
    	String text = edit.getText().toString();
    	if(text.equals("")){
    		return;
    	}
    	String [] newVal = new String[values.length+1];
    	int i = 0;
    	for(String t: values){
    		newVal[i++] = t;
    	}
    	newVal[i] = text;
    	values=newVal;
    	makeMenu();
    }
    
    public void makeMenu(){
    	LinearLayout layout = new LinearLayout(this);
    	android.view.ViewGroup.LayoutParams layPar = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    	layout.setOrientation(LinearLayout.VERTICAL);
    	layout.setLayoutParams(layPar);
    	ListView list = new ListView(layout.getContext());
    	android.view.ViewGroup.LayoutParams listPar = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
    	list.setLayoutParams(listPar);
        list.setAdapter(new ArrayAdapter<String>(this, R.layout.checklist_item,values));
        EditText edit = new EditText(layout.getContext());
        LayoutParams editPar = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        edit.setLayoutParams(editPar);
        String hint = "Add New Task";
        edit.setHint(hint);
		edit.setOnKeyListener(keyListen );
        layout.addView(list);
        layout.addView(edit);
        setContentView(layout);
    }
}
